//
//  CoreDataHandle.m
//  流年
//
//  Created by lanou3g on 15/12/24.
//  Copyright © 2015年 魏大宇. All rights reserved.
//

#import "CoreDataHandle.h"
#import <CoreData/CoreData.h>
static CoreDataHandle *_dataHandle =nil;

@implementation CoreDataHandle
+(instancetype)shareCoreDataHandle{
    return [[self alloc]init];
    
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _dataHandle = [super allocWithZone:zone];
        
    });
    return _dataHandle;
    
}
-(void)getCoreData{
    //创建一个    NSManagedObjectModel对象(描述实体模型的)
    NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
    //2,桥梁
    NSPersistentStoreCoordinator *storeCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
    //存储的路径
    NSString *path =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"data"];
    //存储位置连接类
    //第一个存储的类型(NSSQLiteStoreType 数据库模式)
    //第三个路径
    NSError *error = nil;//options 如果支持版本迁移需要加参数
    //第一个key值 自动迁移旧版本数据 第二个key值自动匹配模型
    NSDictionary *dict = @{NSMigratePersistentStoresAutomaticallyOption:[NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption:[NSNumber numberWithBool:YES]};
    
    [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:path] options: dict error:&error ];
    //3初始化管理上下文
    self.context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
    //上下文管理 连接器
    self.context.persistentStoreCoordinator = storeCoordinator;


}



@end
